package com.example.review;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.review.LitePal.Book;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.List;

public class chapterSix5Activity extends AppCompatActivity {

    private static final String TAG = "chapterSix5Activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter_six5);
        Button button=findViewById(R.id.cre_da);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建数据库
                LitePal.getDatabase();
            }
        });

        /**
         * 添加数据
         */
        Button button1=findViewById(R.id.add_da);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Book book=new Book("李清民",19.99,999,"Java学习","新华社");
                book.save();
            }
        });

        /**
         * 更新数据
         */
        Button button2=findViewById(R.id.upd_da);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Book book=new Book();
                book.setAuthor("张三");
                book.updateAll("pages = ? and price = ?","999","19.99");
            }
        });


        /**
         * 删除数据
         */
        Button button3=findViewById(R.id.del_da);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                LitePal.deleteAll(Book.class,"pages = ?","999");

            }
        });

        /**
         * 查询数据
         */
        Button button4=findViewById(R.id.que_da);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                List<Book> bookList = LitePal.findAll(Book.class);
//                LitePal.select("id","name")
//                        .where("pages = ?","999")
//                        .order("price desc")
//                        .limit(5)
//                        .find(Book.class);


                //Cursor cursor = LitePal.findBySQL("select id,name from Book where pages = ? and price > ?", "100", "123");

                // 查询某些列
                for (Book book : bookList) {
                    Log.d(TAG, " "+book.getAuthor()+book.getName()+book.getPress()+book.getId());
                }
            }
        });

    }
}